﻿using Abp.Configuration;
using Abp.Net.Mail;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.Linq;

namespace Mt.Site.Core.Configuration
{
    public class MtAppSettingProvider : SettingProvider
    {
        private readonly IConfigurationRoot _appConfiguration;

        public MtAppSettingProvider(IAppConfigurationAccessor configurationAccessor)
        {
            _appConfiguration = configurationAccessor.Configuration;
        }

        public override IEnumerable<SettingDefinition> GetSettingDefinitions(SettingDefinitionProviderContext context)
        {
            // Change scope of Email settings
            ChangeEmailSettingScopes(context);
            
            return GetSharedSettings()
                .Union(GetTenantSettings());
        }


        private IEnumerable<SettingDefinition> GetSharedSettings()
        {
            return new[]
            {
                new SettingDefinition(MtAppSettings.General.EMail,"", 
                    scopes: SettingScopes.Application | SettingScopes.Tenant, isVisibleToClients: true),
                new SettingDefinition(MtAppSettings.General.Telephone,"", 
                    scopes: SettingScopes.Application | SettingScopes.Tenant, isVisibleToClients: true),
                new SettingDefinition(MtAppSettings.General.Address,"", 
                    scopes: SettingScopes.Application | SettingScopes.Tenant, isVisibleToClients: true),
                new SettingDefinition(MtAppSettings.General.CopyRight,"", 
                    scopes: SettingScopes.Application | SettingScopes.Tenant, isVisibleToClients: true),
                new SettingDefinition(MtAppSettings.General.ICP,"",
                    scopes: SettingScopes.Application | SettingScopes.Tenant, isVisibleToClients: true),
                new SettingDefinition(MtAppSettings.General.CaseNumber,"",
                    scopes: SettingScopes.Application | SettingScopes.Tenant, isVisibleToClients: true),

                new SettingDefinition(MtAppSettings.General.RecordClicks,"true",
                    scopes: SettingScopes.Application, isVisibleToClients: true),
                new SettingDefinition(MtAppSettings.General.CacheListResult,"false",
                    scopes: SettingScopes.Application, isVisibleToClients: true),

                new SettingDefinition(MtAppSettings.Security.UserPasswordExpires,"false",
                    scopes: SettingScopes.Application | SettingScopes.Tenant, isVisibleToClients: true),

                new SettingDefinition(MtAppSettings.Security.MaximumNumberOfDaysBetweenPasswordChange,"90",
                    scopes: SettingScopes.Application | SettingScopes.Tenant, isVisibleToClients: true),
            };
        }

        private IEnumerable<SettingDefinition> GetTenantSettings()
        {
            return new[]
            {
                 new SettingDefinition(MtAppSettings.Email.UseHostDefaultEmailSettings,
                    GetFromAppSettings(MtAppSettings.Email.UseHostDefaultEmailSettings,
                        MtSiteConsts2.MultiTenancyEnabled ? "true" : "false"), scopes: SettingScopes.Tenant),
                 new SettingDefinition(MtAppSettings.TenantManagement.JiaRi,  "",
                    scopes: SettingScopes.Tenant, isVisibleToClients: true),
                 new SettingDefinition(MtAppSettings.TenantManagement.TiaoXiu,  "",
                    scopes: SettingScopes.Tenant, isVisibleToClients: true),
                  new SettingDefinition(MtAppSettings.TenantManagement.HomeSetGrey, "false",
                    scopes: SettingScopes.Tenant, isVisibleToClients: true),
            };
        }

        private void ChangeEmailSettingScopes(SettingDefinitionProviderContext context)
        {
            if (!MtSiteConsts2.AllowTenantsToChangeEmailSettings)
            {
                //框架里默认值是 SettingScopes.Application | SettingScopes.Tenant
                context.Manager.GetSettingDefinition(EmailSettingNames.Smtp.Host).Scopes = SettingScopes.Application;
                context.Manager.GetSettingDefinition(EmailSettingNames.Smtp.Port).Scopes = SettingScopes.Application;
                context.Manager.GetSettingDefinition(EmailSettingNames.Smtp.UserName).Scopes =
                    SettingScopes.Application;
                context.Manager.GetSettingDefinition(EmailSettingNames.Smtp.Password).Scopes =
                    SettingScopes.Application;
                context.Manager.GetSettingDefinition(EmailSettingNames.Smtp.Domain).Scopes = SettingScopes.Application;
                context.Manager.GetSettingDefinition(EmailSettingNames.Smtp.EnableSsl).Scopes =
                    SettingScopes.Application;
                context.Manager.GetSettingDefinition(EmailSettingNames.Smtp.UseDefaultCredentials).Scopes =
                    SettingScopes.Application;
                context.Manager.GetSettingDefinition(EmailSettingNames.DefaultFromAddress).Scopes =
                    SettingScopes.Application;
                context.Manager.GetSettingDefinition(EmailSettingNames.DefaultFromDisplayName).Scopes =
                    SettingScopes.Application;
            }
        }

        private string GetFromAppSettings(string name, string defaultValue = null)
        {
            return GetFromSettings("App:" + name, defaultValue);
        }

        private string GetFromSettings(string name, string defaultValue = null)
        {
            return _appConfiguration[name] ?? defaultValue;
        }
    }
}
